//
//	File Name   :	Dx10IndexBuffer.cpp
//	Description :	Dx10IndexBuffer implementation file.
//	Author		:	William McVicar
//  Mail		:	mcvicar.william@gmail.com
//

//  Precompiled Header
#include "CommonDefines.h"

#ifdef DX10

//  Library Includes

//  Local Includes
#include "ProteinFramework.h"
#include "IRenderer.h"
#include "Dx10RendererData.h"

//  This includes
#include "Dx10IndexBuffer.h"

//	Macros

//  Static Variables

//  Static Function Prototypes

//  Implementation

namespace Protein
{

IndexBuffer::IndexBuffer( const IndexBufferBase* _pBuffer )
: m_uiOffset( _pBuffer->Offset() )
{
	D3D10_BUFFER_DESC bd;
	bd.Usage = D3D10_USAGE_DEFAULT;
	bd.ByteWidth = _pBuffer->ElementSize() * _pBuffer->NumElements();
	bd.CPUAccessFlags = 0;
	bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
	bd.MiscFlags = 0;

	D3D10_SUBRESOURCE_DATA sd = {0};
	sd.pSysMem = _pBuffer->Data();

	HRESULT hr = renderer().GetRendererData()->m_pDevice->CreateBuffer( &bd, &sd, &m_pIndexBuffer );
	PROTEIN_UNUSED( hr );
}

IndexBuffer::~IndexBuffer()
{
	if( m_pIndexBuffer )
	{
		m_pIndexBuffer->Release();
		m_pIndexBuffer = NULL;
	}
}

void IndexBuffer::Enable()
{
	renderer().GetRendererData()->m_pDevice->IASetIndexBuffer( m_pIndexBuffer, DXGI_FORMAT_R32_UINT, m_uiOffset );
}

void IndexBuffer::Disable()
{
#ifdef _DEBUG
	ID3D10Buffer* pActiveBuffer = NULL;
	DXGI_FORMAT format;
	u32 uiOffset;
	renderer().GetRendererData()->m_pDevice->IAGetIndexBuffer( &pActiveBuffer, &format, &uiOffset );
	assert( pActiveBuffer == m_pIndexBuffer && "This index buffer isn't active" );
#endif
	renderer().GetRendererData()->m_pDevice->IASetIndexBuffer( NULL, DXGI_FORMAT_UNKNOWN, 0 );
}

}

#endif